package com.google.sydym6.view.activities;

import com.google.sydym6.R;
import com.google.sydym6.controllers.IPlayRadioController;
import com.google.sydym6.controllers.PlayRadioController;
import com.google.sydym6.logic.IRadioScrobblerModel;
import com.google.sydym6.logic.RadioScrobblerModel;
import com.google.sydym6.logic.managers.RadioManager;
import com.google.sydym6.logic.observers.IPlayerStateObservable;
import com.google.sydym6.logic.observers.IPlayerStateObserver;
import com.google.sydym6.logic.observers.IRecognizeStatusObservable;
import com.google.sydym6.logic.observers.IRecognizeStatusObserver;
import com.google.sydym6.view.adapters.RadioPlayerPagerAdapter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class RadioPlayerActivity extends AbstractMiniPlayerActivity {

	  @Override
	  protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.radio_player_layout);
	    
	    ImageButton radioPlayPauseButton = (ImageButton) findViewById(R.id.radioPlayPauseButton);
	    radioPlayPauseButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				System.out.println("radio playPauseButton was clicked");
				
				controller.playStopRadio();
			}
		});
	    
	    pager = (ViewPager) findViewById(R.id.radioPlayerPager);
	    
	    initializeMiniPlayer();
	  }
	  
	  @Override
	  protected void onResume() {
		  super.onResume();
		    
		  pagerAdapter = new RadioPlayerPagerAdapter(getSupportFragmentManager());
		  pager.setAdapter(pagerAdapter);

		  pager.setOnPageChangeListener(new OnPageChangeListener() {

			  @Override
			  public void onPageSelected(int position) {
				  Log.d(TAG, "onPageSelected, position = " + position);
			  }

			  @Override
			  public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
			  }

			  @Override
			  public void onPageScrollStateChanged(int state) {
			  }
		  });
	  }
	  
	  @Override
	  public void updatePlayerState() {
		  super.updatePlayerState();
		  
		  runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
		  		ImageButton playPauseButton = (ImageButton) findViewById(R.id.radioPlayPauseButton);
		  		if(model.getRadioManager().isPlaying()) {
		  			playPauseButton.setImageResource(android.R.drawable.ic_media_pause);
		  			
		  			ProgressBar radioLoadingBar = (ProgressBar) findViewById(R.id.songLoading);
					radioLoadingBar.setVisibility(View.VISIBLE);
		  		} else {
		  			playPauseButton.setImageResource(android.R.drawable.ic_media_play);
		  			
		  			ProgressBar radioLoadingBar = (ProgressBar) findViewById(R.id.songLoading);
					radioLoadingBar.setVisibility(View.GONE);
		  		}
			}
		});
	  }
	  
	  @Override
	  public void updateRecognizeStatus() {
		  super.updateRecognizeStatus();
		  
		  runOnUiThread(new Runnable() {

			  @Override
			  public void run() {
				  TextView songTextView = (TextView) findViewById(R.id.radioCurrentSongTextView);
				  songTextView.setVisibility(View.VISIBLE);
				  
				  TextView statusTextView = (TextView) findViewById(R.id.radioCurrentSongLabel);
				  statusTextView.setVisibility(View.VISIBLE);
				  
				  ImageButton playPauseButton = (ImageButton) findViewById(R.id.radioPlayPauseButton);
			  		
				  String status = model.getRadioManager().getRecognizeStatus();
				  System.out.println("updateRecognizeStatus: " + status);
			  		
				  if(status != null) {
					  ProgressBar radioLoadingBar = (ProgressBar) findViewById(R.id.songLoading);
					  if (status.equals(RadioManager.RADIO_LOADING)) {
						  radioLoadingBar.setVisibility(View.VISIBLE);
						  playPauseButton.setVisibility(View.GONE);
						  
					  } else {
						  radioLoadingBar.setVisibility(View.GONE);
						  playPauseButton.setVisibility(View.VISIBLE);
					  }
					  if(status.equals(RadioManager.RECOGNIZING_SUCCESS) || status.equals("Stopped")) {
						  statusTextView.setText(getString(R.string.SONG_RECOGNIZE_STATUS));
						  songTextView.setText(model.getRadioManager().getRecognizedArtistTitle());
					  } else { 
						  statusTextView.setText(getString(R.string.SONG_RECOGNIZE_STATUS) + status);
					  }
				  }
			  }
		  });
	  }
}
